package com.weipu.common.facade.content.api;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.text.TextUtils;
import android.util.Log;

import com.weipu.common.DXApplication;
import com.weipu.common.facade.content.BaseService;
import com.weipu.common.facade.content.ContentProviderFactory;
import com.weipu.common.facade.content.ContentURI;
import com.weipu.common.facade.content.colum.HomeModuleDataColumn;
import com.weipu.common.facade.content.impl.HomeDataContentProviderImpl;
import com.weipu.common.facade.model.HomeModuleModel;
import com.weipu.common.facade.model.ModuleChildrenModel;
import com.weipu.common.util.Logger;

public class HomeModuleDataService extends BaseService {
	private static final String TAG="HomeModuleDataService";

	String activeId;
	String activeName;
	int activeType;
	long activeSort;
	String activeLinkUrl;
	String activePhoneImage;
	String activePadImage;
	int activeSku;
	int activeLinkType;

	/**
	 * 根据模块类型查询模块数据
	 * 
	 * @param context
	 * @param moduleType
	 *            模块类型
	 * @return
	 */
	// public ArrayList<HomeModuleDataLocalModel> queryModuleDataByModuleType(
	// Context context, String moduleType) {
	// Cursor cursor = null;
	// if (TextUtils.isEmpty(moduleType)) {
	// cursor = context.getContentResolver().query(
	// ContentURI.HomeModuleData.CONTENT_URI_ALL,
	// HomeModuleDataColumn.QUERY_HOME_MUDULE_DATA_PROJECTION,
	// null, null, null);
	// } else {
	// cursor = context.getContentResolver().query(
	// ContentURI.HomeModuleData.CONTENT_URI_ALL,
	// HomeModuleDataColumn.QUERY_HOME_MUDULE_DATA_PROJECTION,
	// "module_type=?", new String[] { moduleType }, null);
	// }
	//
	// Logger.d("--cursor--", "--------" + cursor);
	// ArrayList<HomeModuleDataLocalModel> homeModuledlModels = new
	// ArrayList<HomeModuleDataLocalModel>();
	// if (cursor == null) {
	//
	// } else {
	//
	// cursor.moveToFirst();
	//
	// // 数据集合不为空
	// if (cursor.getCount() > 0) {
	// do {
	// HomeModuleDataLocalModel hmdlModel = new HomeModuleDataLocalModel();
	// hmdlModel.setActiveId(cursor.getString(1));
	// hmdlModel.setActiveName(cursor.getString(2));
	// hmdlModel.setActiveSort(cursor.getString(3));
	// hmdlModel.setActiveType(cursor.getString(4));
	// hmdlModel.setActiveLinkUrl(cursor.getString(5));
	// hmdlModel.setActivePhoneImag(cursor.getString(6));
	// hmdlModel.setActivePadImag(cursor.getString(7));
	// hmdlModel.setActiveSku(cursor.getString(8));
	// hmdlModel.setActiveLinkType(cursor.getString(9));
	// hmdlModel.setModuleName(cursor.getString(10));
	// hmdlModel.setModuleSort(cursor.getString(11));
	// hmdlModel.setModuleType(cursor.getString(12));
	// hmdlModel.setModuleIsShow(cursor.getString(13));
	// homeModuledlModels.add(hmdlModel);
	// // 后移
	// } while (cursor.moveToNext());
	// }
	// cursor.close();
	// }
	// return homeModuledlModels;
	// }

	/**
	 * 根据模块类型查询模块数据
	 * 
	 * @param context
	 * @param moduleType
	 *            模块类型
	 * @return
	 */
	public List<HomeModuleModel> queryModuleDataByModuleType(Context context,
			String moduleType) {
		Cursor cursor = null;
		if (TextUtils.isEmpty(moduleType)) {
			cursor = context.getContentResolver().query(
					ContentURI.HomeModuleData.CONTENT_URI_ALL,
					HomeModuleDataColumn.QUERY_HOME_MUDULE_DATA_PROJECTION,
					null, null, null);
		} else {
			cursor = context.getContentResolver().query(
					ContentURI.HomeModuleData.CONTENT_URI_ALL,
					HomeModuleDataColumn.QUERY_HOME_MUDULE_DATA_PROJECTION,
					"module_type=?", new String[] { moduleType }, null);
		}

		Logger.d("--cursor--", "--------" + cursor);
		List<HomeModuleModel> hmmodels =null;
		HomeModuleModel moduletype1 = null;
		HomeModuleModel moduletype2 = null;
		HomeModuleModel moduletype3 = null;
		HomeModuleModel moduletype4 = null;
		List<ModuleChildrenModel> module1Chidren1 = null;
		List<ModuleChildrenModel> module1Chidren2 = null;
		List<ModuleChildrenModel> module1Chidren3 = null;
		List<ModuleChildrenModel> module1Chidren4 = null;
		if (cursor == null) {

		} else {

			cursor.moveToFirst();

			// 数据集合不为空
			if (cursor.getCount() > 0) {
				Logger.d("--cursor--", "--------" + cursor.getCount());
				hmmodels= new ArrayList<HomeModuleModel>();
				do {
					activeId = cursor.getString(1);
					activeName = cursor.getString(2);
					activeSort = cursor.getLong(3);
					activeType = cursor.getInt(4);
					activeLinkUrl = cursor.getString(5);
					activePhoneImage = cursor.getString(6);
					activePadImage = cursor.getString(7);
					activeSku = cursor.getInt(8);
					activeLinkType = cursor.getInt(9);
					String moduleName = cursor.getString(10);
					long moduleSort = cursor.getLong(11);
					int moduletype = cursor.getInt(12);
					int moduleIsShow = cursor.getInt(13);
					ModuleChildrenModel mochModel = new ModuleChildrenModel();
					switch (moduletype) {
					case 1:
						if (null == moduletype1) {
							module1Chidren1 = new ArrayList<ModuleChildrenModel>();
							moduletype1 = new HomeModuleModel();
							moduletype1.setIsShow(moduleIsShow == 1 ? true
									: false);
							moduletype1.setName(moduleName);
							moduletype1.setSort(moduleSort);
							moduletype1.setModuleType(moduletype);
						}
						module1Chidren1.add(setModuleChildrenModel(mochModel));
						break;
					case 2:
						if (null == moduletype2) {
							module1Chidren2 = new ArrayList<ModuleChildrenModel>();
							moduletype2 = new HomeModuleModel();
							moduletype2.setIsShow(moduleIsShow == 1 ? true
									: false);
							moduletype2.setName(moduleName);
							moduletype2.setSort(moduleSort);
							moduletype2.setModuleType(moduletype);
						}
						module1Chidren2.add(setModuleChildrenModel(mochModel));
						break;
					case 3:
						if (null == moduletype3) {
							module1Chidren3 = new ArrayList<ModuleChildrenModel>();
							moduletype3 = new HomeModuleModel();
							moduletype3.setIsShow(moduleIsShow == 1 ? true
									: false);
							moduletype3.setName(moduleName);
							moduletype3.setSort(moduleSort);
							moduletype3.setModuleType(moduletype);
						}
						module1Chidren3.add(setModuleChildrenModel(mochModel));
						break;
					case 4:
						if (null == moduletype4) {
							module1Chidren4 = new ArrayList<ModuleChildrenModel>();
							moduletype4 = new HomeModuleModel();
							moduletype4.setIsShow(moduleIsShow == 1 ? true
									: false);
							moduletype4.setName(moduleName);
							moduletype4.setSort(moduleSort);
							moduletype4.setModuleType(moduletype);
						}
						module1Chidren4.add(setModuleChildrenModel(mochModel));
						break;
					default:
						break;
					}
					// 后移
				} while (cursor.moveToNext());
				if(null!=moduletype1){
					moduletype1.setChildren(module1Chidren1);
					hmmodels.add(moduletype1);
				}
				if(null!=moduletype2){
					moduletype2.setChildren(module1Chidren2);
					hmmodels.add(moduletype2);
				}
				if(null!=moduletype3){
					moduletype3.setChildren(module1Chidren3);
					hmmodels.add(moduletype3);
				}
				if(null!=moduletype4){
					moduletype4.setChildren(module1Chidren4);
					hmmodels.add(moduletype4);
				}
			}
			
			cursor.close();
		}
		return hmmodels;
	}

	private ModuleChildrenModel setModuleChildrenModel(ModuleChildrenModel model) {
		model.setActiveID(activeId);
		model.setName(activeName);
		model.setSort(activeSort);
		model.setActiveType(activeType);
		model.setPhoneImg(activePhoneImage);
		model.setPadImg(activePadImage);
		model.setLinkType(activeLinkType);
		model.setLinkUrl(activeLinkUrl);
		model.setSku(activeSku);
		return model;
	}

	/**
	 * 删除所有广告数据
	 * 
	 */
	public int clearHomeModuleData() {

		int returnint = DXApplication.getContext().getContentResolver()
				.delete(ContentURI.HomeModuleData.CONTENT_URI_ALL, null, null);

		if (Logger.isLog) {
			Logger.d("clearHomeModuleData() - end, returnint: " + returnint);
		}
		return returnint;

	}
	

	/**
	 * 对象转换为map
	 * 
	 * @return [参数说明]
	 * @return ContentValues [返回类型说明]
	 * @exception throws [违例类型] [违例说明]
	 * @see [类、类#方法、类#成员]
	 */
	public ContentValues template2map(List<HomeModuleModel> hmmodels) {
		ContentValues map = new ContentValues();

//		if (null != hmmodels) {
//			for (int i = 0; i < hmmodels.size(); i++) {
//				HomeModuleModel hmm = hmmodels.get(i);
//				List<ModuleChildrenModel> mcms = hmm.getChildren();
//				map
//				for (int j = 0; j < mcms.size(); j++) {
//
//				}
//			}
//			map.put(SearchHistoryColumn.HISTORYNAME, name);
//			map.put(SearchHistoryColumn.SEARCH_HISTORY_TIME, searchTime);
//		}
		return map;
		
//    	    

	}
	
	/**
	 * 保存首页数据
	 * @param hmms
	 * @return
	 */
    public boolean saveHomeDataInfo(List<HomeModuleModel> hmms)
    {
        if (Logger.isLog)
        {
            Logger.d("saveHomeDataInfo() - start, type: ");
        }
        
        for (int i = 0; i < hmms.size(); i++) {
    		HomeModuleModel hmm=hmms.get(i);
    		List<ModuleChildrenModel> mcms=hmm.getChildren();
    		for (int j = 0; j < mcms.size(); j++) {
    			ContentValues initialValues = new ContentValues();
    			ModuleChildrenModel mcm=mcms.get(j);
//    			ACTIVE_ID, ACTIVE_NAME, ACTIVE_TYPE, ACTIVE_SORT,
//    			 ACTIVE_LINKURL,ACTIVE_PHONEIMG,ACTIVE_PADIMG,ACTIVE_SKU,
//    			 ACTIVE_LINK_TYPE,MODULE_NAME,MODULE_SORT,MODULE_TYPE,MODULE_ISSHOW
//    			 initialValues.put(HomeModuleDataColumn.ACTIVE_ID, mcm.getActiveID());
    	    	 initialValues.put(HomeModuleDataColumn.ACTIVE_NAME, mcm.getName());
    	    	 initialValues.put(HomeModuleDataColumn.ACTIVE_TYPE, mcm.getActiveType());
    	    	 initialValues.put(HomeModuleDataColumn.ACTIVE_SORT, mcm.getSort());
    	    	 initialValues.put(HomeModuleDataColumn.ACTIVE_LINKURL, mcm.getLinkUrl());
    	    	 initialValues.put(HomeModuleDataColumn.ACTIVE_PHONEIMG, mcm.getPhoneImg());
    	    	 initialValues.put(HomeModuleDataColumn.ACTIVE_PADIMG, mcm.getPadImg());
    	    	 initialValues.put(HomeModuleDataColumn.ACTIVE_SKU, mcm.getSku());
    	    	 initialValues.put(HomeModuleDataColumn.ACTIVE_LINK_TYPE, mcm.getLinkType());
    	    	 initialValues.put(HomeModuleDataColumn.MODULE_NAME, hmm.getName());
    	    	 initialValues.put(HomeModuleDataColumn.MODULE_SORT, hmm.getSort());
    	    	 initialValues.put(HomeModuleDataColumn.MODULE_TYPE, hmm.getModuleType());
    	    	 initialValues.put(HomeModuleDataColumn.MODULE_ISSHOW, hmm.isIsShow()?1:0);
    	    	  try
    	          {
    	              DXApplication.getContext()
    	                  .getContentResolver()
    	                  .insert(ContentURI.HomeModuleData.CONTENT_URI_ALL, initialValues);
    	          }
    	          catch (Exception e)
    	          {
    	              Logger.e(TAG, "saveHomeDataInfo error.", e);
    	              return false;
    	          }
    	          
    		}
        }
        
        return true;
    }
    
    /**
     * 通过事物 批量插入数据
     * @param hmms
     */
    public void batchInsertDataByTransaction(List<HomeModuleModel> hmms){
    	HomeDataContentProviderImpl homeDataCP=(HomeDataContentProviderImpl) ContentProviderFactory.getContentProvier(ContentURI.HomeModuleData.CONTENT_URI_ALL);
    	if(null==homeDataCP){
    		Log.i(TAG, "batchInsertDataByTransaction is null");
    		return;
    	}
    	homeDataCP.batchInsertHomeData(hmms);
    }
}
